﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZFS.Library;
using ZFS.Model;
using ZFSInterface.User;

namespace ZFSData.Manager
{

    /// <summary>
    /// 用户管理器 
    /// </summary>
    public class UserManager : BaseDal<tb_User>, IUser
    {
        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="account"></param>
        public void Logout(string account)
        {
            using (var db = new ZFSConfig())
            {
                var model = db.tb_User.FirstOrDefault(t => t.Account.Equals(account));
                model.FlagOnline = "0";
                DbEntityEntry dbEntity = db.Entry<tb_User>(model);
                dbEntity.State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 登录系统
        /// </summary>
        /// <param name="Account">账号</param>
        /// <param name="Password">密码</param>
        /// <returns>信息</returns>
        public tb_User Login(string Account, string Password)
        {
            using (var db = new ZFSConfig())
            {
                var userList = db.View_UserAuthority.Where(t => t.Account.Equals("admin")).ToList();

                return db.tb_User.FirstOrDefault(q => q.Account.Equals(Account) && q.Password.Equals(Password));
            }
        }

        /// <summary>
        /// 获取用户权限
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public List<View_UserAuthority> GetAuthority(string account)
        {
            using (var db = new ZFSConfig())
            {
                return db.View_UserAuthority.Where(t => t.Account.Equals(account)).ToList();
            }
        }

        public tb_User GetModelByAccount(string account)
        {
            using (var db = new ZFSConfig())
            {
                return db.tb_User.AsNoTracking().FirstOrDefault(q => q.Account.Equals(account));
            }
        }

        public List<tb_User> GetModels(tb_User search)
        {
            return base.GetModels(ExpressionHelper.GenerateQueryExp(search));
        }

        public List<tb_User> GetPagingModels(int pageIndex, int pageSize, out int totalCount, tb_User search, bool Asc = false)
        {
            Expression<Func<tb_User, int>> orderexp = x => x.isid;
            return base.GetPagingModels(pageIndex, pageSize,
                out totalCount,
                ExpressionHelper.GenerateQueryExp(search), orderexp,
                Asc);
        }

        public bool ExistEntity(tb_User entity)
        {
            return base.ExistEntity(ExpressionHelper.GenerateQueryExp(entity));
        }
    }
}
